using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace CCEmailService
{
	public class CCMailService : System.ServiceProcess.ServiceBase
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		//private System.Diagnostics.EventLog eventLog1 = null;
		private Thread mailThread = null; 

		public CCMailService()
		{
			// This call is required by the Windows.Forms Component Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitComponent call
			if (!System.Diagnostics.EventLog.SourceExists("CCEmailService"))
			{
				System.Diagnostics.EventLog.CreateEventSource("CCEmailService", "Application");
			}
			//eventLog1.Source = "CCEmailService";
			//eventLog1.Log = "Application";
		}

		// The main entry point for the process
		static void Main()
		{
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CCMailService() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);

            //CCMailService service = new CCMailService();
            //Console.WriteLine("before send");
            //service.SendMail();
            //Console.WriteLine("after send");


			//			CCMailService mailService = new CCMailService();
			//			mailService.OnStart(null);
			//			Console.ReadLine();
			//			mailService.OnStop();
		}

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			//this.eventLog1 = new System.Diagnostics.EventLog();
			//((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
			// 
			// CCMailService
			// 
			this.ServiceName = "ASBCCMailService";
			//((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		protected override void OnStart(string[] args)
		{
			// TODO: Add code here to start your service.
			//eventLog1.WriteEntry("CCEmailService is now in OnStart.");

			mailThread = new Thread( new ThreadStart(SendMail));
			mailThread.Start();			
		}
 
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			// TODO: Add code here to perform any tear-down necessary to stop your service.
			//eventLog1.WriteEntry("CCEmailService is now in OnStop.");
			flag = false;

			mailThread.Join(5000);
		
		}

		private int interval = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["Interval"]);
		private string remindTime = System.Configuration.ConfigurationSettings.AppSettings["RemindTime"].ToString();
		private bool flag = true;
        IEmail emailService = BEGDC.Utility.Castle.AppContainer.Instance["EmailService"] as IEmail;
		private void SendMail()
		{
			//int sleepTime = interval * 1000;
			//DateTime startTime;
			//TimeSpan diff;
			//eventLog1.WriteEntry("CCEmailService come in SendMail.");

			
			while(flag)
			{
				try
				{
					//startTime = DateTime.Now;
					emailService.SendEmails();

					//emailService.SendReminder();

                    //if (System.DateTime.Now.ToString("HH:mm") == remindTime)
                    //{
                    //    emailService.SendReminder_Day();
                    //}

					//diff = DateTime.Now - startTime;
					//eventLog1.WriteEntry("CCEmailService ran " + diff.Seconds.ToString() + " seconds in SendMail.");
                    Thread.Sleep(interval * 1000);
				}
				catch (Exception ex)
				{
					//this.eventLog1.WriteEntry(ex.Message);
                    Thread.Sleep(interval * 1000);
				}
			}
			
			Thread.CurrentThread.Abort();
		}
		
	}
}
